package com.sgb.kjwl.view.ui.activity.pick;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.MapView;
import com.sgb.alimaplibrary.MapMarkerTools;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPickCompanyLocationMapBinding;
import com.sgb.kjwl.view.widget.pick.PickMapAppWindow;
import com.swgk.core.util.MToast;


public class PickCompanyLocationMapActivity extends AppActivity {

    private ActivityPickCompanyLocationMapBinding binding;

    public MapMarkerTools mapMarkerTools;
    private MapView mMapView;
    private Double latD = 0.0;
    private Double logD = 0.0;
    private String address;
    private String comName;
    private PickMapAppWindow pickMapAppWindow;
    private boolean isShowBaidu;
    private boolean isShowGaode;
    private boolean isShowTenxun;

    public static void start(Context context, String lat, String lag) {
        Intent intent = new Intent();
        intent.setClass(context, PickCompanyLocationMapActivity.class);
        intent.putExtra("lat", lat);
        intent.putExtra("lag", lag);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, String lat, String lag, String address, String comName) {
        Intent intent = new Intent();
        intent.setClass(context, PickCompanyLocationMapActivity.class);
        intent.putExtra("lat", lat);
        intent.putExtra("lag", lag);
        intent.putExtra("address", address);
        intent.putExtra("comName", comName);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapMarkerTools = new MapMarkerTools(getActivity(), getLifecycle());
        initMap(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_company_location_map);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("查看地图");
    }

    @Override
    protected void init() {
        String lat = getIntent().getStringExtra("lat");
        String lag = getIntent().getStringExtra("lag");
        address = getIntent().getStringExtra("address");
        comName = getIntent().getStringExtra("comName");
        latD = Double.valueOf(lat);
        logD = Double.valueOf(lag);
    }

    private void initMap(Bundle savedInstanceState) {
        mMapView = binding.mapView;

        if (mMapView.getChildCount() >= 2) {
            View child = mMapView.getChildAt(1);
            if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
                child.setVisibility(View.INVISIBLE);
            }
        }
        mapMarkerTools.setMapLocationStyle(R.mipmap.pick_location_tag);
        mapMarkerTools.init(savedInstanceState, mMapView);
        mapMarkerTools.setMarkerClick(new MapMarkerTools.MarkerClick() {


            @Override
            public void onMarkerClick(int pos, String title) {
            }

            @Override
            public void onLocationChanged(Double currentLat, Double currentLong, String address) {

            }
        });
        mapMarkerTools.setMyLocation(latD, logD, R.mipmap.pick_location_tag);
        binding.companyName.setText(comName);
        binding.currentPosition.setText(address);
        binding.goButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showHintDialog(PickCompanyLocationMapActivity.this);
            }
        });
    }

    protected void showHintDialog(Context context) {
        if (isAvilible(context, "com.baidu.BaiduMap")) {
            isShowBaidu = true;
        }
        if (isAvilible(context, "com.autonavi.minimap")) {
            isShowGaode = true;
        }
        if (isAvilible(context, "com.tencent.map")) {
            isShowTenxun = true;
        }
        if (!isShowBaidu && !isShowGaode && !isShowTenxun) {
            MToast.showLongToast1(context, "未检索到地图App", Gravity.CENTER);
            return;
        }
        showPopwindow(isShowBaidu, isShowGaode, isShowTenxun);
    }

    protected void showPopwindow(boolean showBaidu, boolean showGaode, boolean showTenxun) {
        pickMapAppWindow = new PickMapAppWindow(getActivity(), 3);
        pickMapAppWindow.setBgView(binding.grayLayout);
        pickMapAppWindow.showPopwindowBottom(binding.mapView);
        pickMapAppWindow.setData(latD, logD, address);
        pickMapAppWindow.setShowData(showBaidu, showGaode, showTenxun);
    }

}
